package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "购物车相关接口")
@RestController("userShoppingCart")
@RequestMapping("/user/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Resource
    ShoppingCartService shoppingCartService;


    @ApiOperation("新增购物车")
    @PostMapping("/add")
    public Result add(@RequestBody ShoppingCartDTO dto){
        log.info("添加购物车：{}",dto);
        shoppingCartService.add(dto);
        return Result.success();
    }

    @ApiOperation("查看购物车")
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        log.info("查看购物车：{}" , currentId);
        List<ShoppingCart> result =  shoppingCartService.list(currentId);
        return Result.success(result);
    }

    @ApiOperation("删除购物车中一个商品")
    @PostMapping("/sub")
    public Result subOne(@RequestBody ShoppingCartDTO dto){
        log.info("删除一个购物车中的商品");
        shoppingCartService.subOne(dto);
        return Result.success();
    }

    @ApiOperation("情况购物车")
    @DeleteMapping("clean")
    public Result clean(){
        Long currentId = BaseContext.getCurrentId();
        log.info("删除购物车：{}" , currentId);
        shoppingCartService.delAll(currentId);
        return Result.success();
    }

}
